1
2
3
4
5
6
7 package ca.uhn.cache.impl;
8
9 import java.util.Date;
10 import java.util.Iterator;
11
12 import junit.framework.TestCase;
13 import ca.uhn.cache.IDimension;
14 import ca.uhn.cache.IQuery;
15 import ca.uhn.cache.IQueryParam;
16 import ca.uhn.cache.internal.impl.ParamSpaceConfig;
17
18 /***
19 * Unit tests for <code>ParamSpace</code>.
20 *
21 * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
22 * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:51:48 $ by $Author: bryan_tripp $
23 */
24 public class ParamSpaceTest extends TestCase {
25
26 /***
27 */
28 public void testGetDimensions() {
29 Dimension dim = new Dimension("foo", new Class[] {StringParam.class});
30 ParamSpaceConfig config =
31 new ParamSpaceConfig( new IDimension[] {dim}, new IQueryParam[] {}, new IQueryParam[] {} );
32 ParamSpace p = ParamSpace.createInstance( config );
33 assertEquals(dim, p.getDimensions()[0]);
34 }
35
36 /***
37 */
38 public void testChunkString() {
39 Dimension dim = new Dimension("foo", new Class[] {StringParam.class, StringSetParam.class});
40 ParamSpaceConfig config =
41 new ParamSpaceConfig( new IDimension[] {dim}, new IQueryParam[] {}, new IQueryParam[] {} );
42 ParamSpace space = ParamSpace.createInstance( config );
43
44 StringParam param1 = new StringParam(dim, "a");
45 StringParam param2 = new StringParam(dim, "b");
46 StringParam param3 = new StringParam(dim, "c");
47
48 assertEquals(1, space.chunk(param1).length);
49 assertEquals(param1, space.chunk(param1)[0]);
50 assertEquals(param2, space.chunk(param2)[0]);
51 assertEquals(param3, space.chunk(param3)[0]);
52
53 StringSetParam chunk1 = new StringSetParam(dim, new String[] {"a", "b"});
54 StringSetParam chunk2 = new StringSetParam(dim, new String[] {"c", "d"});
55
56 config = new ParamSpaceConfig(
57 new IDimension[] {dim},
58 new IQueryParam[] { chunk1, chunk2 },
59 new IQueryParam[] {} );
60
61 space = ParamSpace.createInstance( config );
62
63 assertEquals(1, space.chunk(param1).length);
64 assertEquals(chunk1, space.chunk(param1)[0]);
65 assertEquals(chunk1, space.chunk(param2)[0]);
66 assertEquals(chunk2, space.chunk(param3)[0]);
67
68 assertEquals(2, space.chunk(new StringSetParam(dim, new String[]{"a", "c"})).length);
69 }
70
71 /***
72 */
73 public void testChunkDate() {
74 Dimension dim = new Dimension("foo", new Class[] {DateParam.class, DateRangeParam.class});
75
76 Date twoWeeksAgo = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 14);
77 Date oneWeekAgo = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 7);
78 Date now = new Date();
79
80 DateRangeParam chunk1 = new DateRangeParam(dim, twoWeeksAgo, oneWeekAgo);
81 DateRangeParam chunk2 = new DateRangeParam(dim, oneWeekAgo, now);
82
83 ParamSpaceConfig config =
84 new ParamSpaceConfig(
85 new IDimension[] {dim},
86 new IQueryParam[] { chunk1, chunk2 },
87 new IQueryParam[] {} );
88
89 ParamSpace space = ParamSpace.createInstance( config );
90
91 Date tenDaysAgo = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 10);
92 Date fiveDaysAgo = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 5);
93 assertEquals(1, space.chunk(new DateParam(dim, twoWeeksAgo)).length);
94 assertEquals(chunk1, space.chunk(new DateParam(dim, twoWeeksAgo))[0]);
95 assertEquals(chunk1, space.chunk(new DateParam(dim, tenDaysAgo))[0]);
96
97 IQueryParam[] result = space.chunk(new DateRangeParam(dim, tenDaysAgo, fiveDaysAgo));
98 assertEquals(2, result.length);
99 }
100
101 /***
102 */
103 public void testGroup1D() {
104 Dimension dim = new Dimension("foo", new Class[] {DateParam.class, DateRangeParam.class});
105
106 long time1 = System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 20;
107 long time2 = System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 15;
108 long time3 = System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 11;
109 long time4 = System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 8;
110 long time5 = System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 6;
111 long time6 = System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 5;
112
113 IQueryParam satPt1 = new DateParam(dim, new Date(1000 * 60 * 60 * 24 * 20) );
114
115 ParamSpaceConfig config =
116 new ParamSpaceConfig(
117 new IDimension[] {dim},
118 new IQueryParam[] {},
119 new IQueryParam[] { satPt1 } );
120 ParamSpace space = ParamSpace.createInstance( config );
121
122 IQuery[] queries = new IQuery[] {
123 get1msDateQuery(dim, time1),
124 get1msDateQuery(dim, time2),
125 get1msDateQuery(dim, time3),
126 get1msDateQuery(dim, time4),
127 get1msDateQuery(dim, time5),
128 get1msDateQuery(dim, time6)};
129
130 IQuery[] grouped = space.group(queries, 3);
131 assertEquals(3, grouped.length);
132 assertEquals(time1, ((DateRangeParam) getFirstParam(grouped[0])).getStart().getTime());
133 assertEquals(time2 + 1, ((DateRangeParam) getFirstParam(grouped[0])).getEnd().getTime());
134 assertEquals(time3, ((DateRangeParam) getFirstParam(grouped[1])).getStart().getTime());
135 assertEquals(time4 + 1, ((DateRangeParam) getFirstParam(grouped[1])).getEnd().getTime());
136 assertEquals(time5, ((DateRangeParam) getFirstParam(grouped[2])).getStart().getTime());
137 assertEquals(time6 + 1, ((DateRangeParam) getFirstParam(grouped[2])).getEnd().getTime());
138
139 grouped = space.group(queries, 4);
140 assertEquals(4, grouped.length);
141 assertEquals(time1, ((DateRangeParam) getFirstParam(grouped[0])).getStart().getTime());
142 assertEquals(time2 + 1, ((DateRangeParam) getFirstParam(grouped[0])).getEnd().getTime());
143 assertEquals(time3, ((DateRangeParam) getFirstParam(grouped[1])).getStart().getTime());
144 assertEquals(time4 + 1, ((DateRangeParam) getFirstParam(grouped[1])).getEnd().getTime());
145 assertEquals(time5, ((DateRangeParam) getFirstParam(grouped[2])).getStart().getTime());
146 assertEquals(time6, ((DateRangeParam) getFirstParam(grouped[3])).getStart().getTime());
147
148 grouped = space.group(queries, 2);
149 assertEquals(2, grouped.length);
150 assertEquals(time1, ((DateRangeParam) getFirstParam(grouped[0])).getStart().getTime());
151 assertEquals(time4 + 1, ((DateRangeParam) getFirstParam(grouped[0])).getEnd().getTime());
152 assertEquals(time5, ((DateRangeParam) getFirstParam(grouped[1])).getStart().getTime());
153 assertEquals(time6 + 1, ((DateRangeParam) getFirstParam(grouped[1])).getEnd().getTime());
154 }
155
156 /***
157 */
158 public void testGroup2D() {
159 Dimension dim1 = new Dimension("date", new Class[] {DateParam.class, DateRangeParam.class});
160 Dimension dim2 = new Dimension("string", new Class[] {StringParam.class, StringSetParam.class});
161
162 IQueryParam satPt1 = new DateParam(dim1, new Date(1000 * 60 * 60 * 24 * 20) );
163
164 ParamSpaceConfig config =
165 new ParamSpaceConfig(
166 new IDimension[] {dim1, dim2},
167 new IQueryParam[] {},
168 new IQueryParam[] { satPt1 } );
169
170 ParamSpace space = ParamSpace.createInstance( config );
171
172 Date date1 = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 20);
173 Date date2 = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 15);
174 String string1 = "a";
175 String string2 = "b";
176
177 IQuery[] queries = new IQuery[] {
178 getDateStringQuery(dim1, date1, dim2, string1),
179 getDateStringQuery(dim1, date2, dim2, string1),
180 getDateStringQuery(dim1, date2, dim2, string2)};
181
182 IQuery[] grouped = space.group(queries, 2);
183 assertEquals(2, grouped.length);
184 DateRangeParam group0Range = (DateRangeParam) getParam(grouped[0], dim1);
185 StringSetParam group0Set = (StringSetParam) getParam(grouped[0], dim2);
186 DateParam group1Range = (DateParam) getParam(grouped[1], dim1);
187 StringParam group1Set = (StringParam) getParam(grouped[1], dim2);
188
189 assertEquals(date1, group0Range.getStart());
190 assertEquals(date2, group0Range.getEnd());
191 assertTrue(group0Set.getValues().contains(string1));
192 assertFalse(group0Set.getValues().contains(string2));
193
194 assertEquals(date2, group1Range.getValue());
195 assertEquals(string2, group1Set.getValue());
196 }
197
198 private IQuery getDateStringQuery(IDimension theDateDimension, Date theDate,
199 IDimension theStringDimension, String theString)
200 {
201 IQuery result = new Query();
202 result.addParameter(new DateParam(theDateDimension, theDate));
203 result.addParameter(new StringParam(theStringDimension, theString));
204 return result;
205 }
206
207 private IQuery get1msDateQuery(IDimension theDimension, long theStartTime) {
208 IQuery result = new Query();
209 result.addParameter(new DateRangeParam(theDimension,
210 new Date(theStartTime), new Date(theStartTime + 1)));
211 return result;
212 }
213
214 private IQueryParam getFirstParam(IQuery theQuery) {
215 return (IQueryParam) theQuery.getParameters().iterator().next();
216 }
217
218 private IQueryParam getParam(IQuery theQuery, IDimension theDimension) {
219 Iterator it = theQuery.getParameters().iterator();
220 IQueryParam result = null;
221 while (it.hasNext() && result == null) {
222 IQueryParam param = (IQueryParam) it.next();
223 if (param.getDimension().equals(theDimension)) {
224 result = param;
225 }
226 }
227 return result;
228 }
229
230 }